package com.cdty.chess.service.impl;

import com.cdty.chess.model.CosProperties;
import com.cdty.chess.model.QrCode;
import com.cdty.chess.service.QrCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class QrCodeServiceImpl implements QrCodeService {

    @Autowired
    NamedParameterJdbcTemplate template;

    @Autowired
    CosProperties cosProperties;

    @Override
    public String addQrCode(QrCode qc) {
        String sql = """
                    insert into qr_code(
                    id,
                    level_one_url,level_two_url,level_three_url,level_four_url
                   )values(
                   REPLACE(UUID(), '-', ''),
                    :level_one_url,:level_two_url,:level_three_url,:level_four_url
                   )
                """;
        template.update(sql,
                new MapSqlParameterSource("level_one_url",qc.getLevel_one_url())
                        .addValue("level_two_url",qc.getLevel_two_url())
                        .addValue("level_three_url",qc.getLevel_three_url())
                        .addValue("level_four_url",qc.getLevel_four_url())
        );

        return "上传成功";
    }

    @Override
    public String updateQrCode(QrCode qc) {
        String sql = """
                    update qr_code
                    set 
                    level_one_url =:level_one_url,
                    level_two_url =:level_two_url,
                    level_three_url =:level_three_url,
                    level_four_url =:level_four_url
                   where id = :id;
                   
                """;
        template.update(sql,
                new MapSqlParameterSource("level_one_url",qc.getLevel_one_url())
                        .addValue("level_two_url",qc.getLevel_two_url())
                        .addValue("level_three_url",qc.getLevel_three_url())
                        .addValue("level_four_url",qc.getLevel_four_url())
                        .addValue("id",qc.getId())
        );
        return "上传成功";
    }

    @Override
    public QrCode getQrCode() {
        String sql = """
                select * from qr_code where 1=1 
                """;
        MapSqlParameterSource param = new MapSqlParameterSource();
        List<QrCode> l = template.query(sql, param, new BeanPropertyRowMapper<>(QrCode.class));
        if(l.size()>0){
            //组装图片
            /*if(StringUtils.hasText(l.get(0).getLevel_one_url())){
                l.get(0).setLevel_one_url(cosProperties.getDomain()+"/"+l.get(0).getLevel_one_url());
            }if(StringUtils.hasText(l.get(0).getLevel_two_url())){
                l.get(0).setLevel_two_url(cosProperties.getDomain()+"/"+l.get(0).getLevel_two_url());
            }if(StringUtils.hasText(l.get(0).getLevel_three_url())){
                l.get(0).setLevel_three_url(cosProperties.getDomain()+"/"+l.get(0).getLevel_three_url());
            }if(StringUtils.hasText(l.get(0).getLevel_four_url())){
                l.get(0).setLevel_four_url(cosProperties.getDomain()+"/"+l.get(0).getLevel_four_url());
            }*/
            return l.get(0);
        }
        return new QrCode();
    }
}
